﻿namespace Innovative.ProXr
{
	/// <summary>
	/// Allows all 16 timers to be resumed or halted by setting the
	/// individual timer to True or False.
	/// </summary>
	public class ActivateTimers
	{
		/// <summary>
		/// Set to True to resume Timer 0 and False to Halt timer.
		/// </summary>
		public bool Timer0 { get; set; }

		/// <summary>
		/// Set to True to resume Timer 1 and False to Halt timer.
		/// </summary>
		public bool Timer1 { get; set; }

		/// <summary>
		/// Set to True to resume Timer 2 and False to Halt timer.
		/// </summary>
		public bool Timer2 { get; set; }

		/// <summary>
		/// Set to True to resume Timer 3 and False to Halt timer.
		/// </summary>
		public bool Timer3 { get; set; }

		/// <summary>
		/// Set to True to resume Timer 4 and False to Halt timer.
		/// </summary>
		public bool Timer4 { get; set; }

		/// <summary>
		/// Set to True to resume Timer 5 and False to Halt timer.
		/// </summary>
		public bool Timer5 { get; set; }

		/// <summary>
		/// Set to True to resume Timer 6 and False to Halt timer.
		/// </summary>
		public bool Timer6 { get; set; }

		/// <summary>
		/// Set to True to resume Timer 7 and False to Halt timer.
		/// </summary>
		public bool Timer7 { get; set; }

		/// <summary>
		/// Set to True to resume Timer 8 and False to Halt timer.
		/// </summary>
		public bool Timer8 { get; set; }

		/// <summary>
		/// Set to True to resume Timer 9 and False to Halt timer.
		/// </summary>
		public bool Timer9 { get; set; }

		/// <summary>
		/// Set to True to resume Timer 10 and False to Halt timer.
		/// </summary>
		public bool Timer10 { get; set; }

		/// <summary>
		/// Set to True to resume Timer 11 and False to Halt timer.
		/// </summary>
		public bool Timer11 { get; set; }

		/// <summary>
		/// Set to True to resume Timer 12 and False to Halt timer.
		/// </summary>
		public bool Timer12 { get; set; }

		/// <summary>
		/// Set to True to resume Timer 13 and False to Halt timer.
		/// </summary>
		public bool Timer13 { get; set; }

		/// <summary>
		/// Set to True to resume Timer 14 and False to Halt timer.
		/// </summary>
		public bool Timer14 { get; set; }

		/// <summary>
		/// Set to True to resume Timer 15 and False to Halt timer.
		/// </summary>
		public bool Timer15 { get; set; }

		/// <summary>
		/// Gets the Most Significant byte sent to the relay controller
		/// </summary>
		public byte Msb
		{
			get
			{
				return (byte)((this.Timer8 ? 1 : 0) +
							 (this.Timer9 ? 2 : 0) +
							 (this.Timer10 ? 4 : 0) +
							 (this.Timer11 ? 8 : 0) +
							 (this.Timer12 ? 16 : 0) +
							 (this.Timer13 ? 32 : 0) +
							 (this.Timer14 ? 64 : 0) +
							 (this.Timer15 ? 128 : 0));
			}
		}

		/// <summary>
		/// Gets the Least Significant byte sent to the relay controller
		/// </summary>
		public byte Lsb
		{
			get
			{
				return (byte)((this.Timer0 ? 1 : 0) +
							 (this.Timer1 ? 2 : 0) +
							 (this.Timer2 ? 4 : 0) +
							 (this.Timer3 ? 8 : 0) +
							 (this.Timer4 ? 16 : 0) +
							 (this.Timer5 ? 32 : 0) +
							 (this.Timer6 ? 64 : 0) +
							 (this.Timer7 ? 128 : 0));
			}
		}
	}

	/// <summary>
	/// Converts a ActivateTimers object to a two byte parameter required for the relay controller
	/// </summary>
	public class ActivateTimerParameterConverter : IParameterConverter<ActivateTimers>
	{
		RelayByte[] IParameterConverter<ActivateTimers>.Convert(ActivateTimers parameter)
		{
			RelayByte[] returnValue = new RelayByte[2];

			returnValue[0] = parameter.Lsb;
			returnValue[1] = parameter.Msb;

			return returnValue;
		}
	}
}
